package com.neuedu.funlifebbs.controller;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageInfo;
import com.neuedu.funlifebbs.bean.Honors;
import com.neuedu.funlifebbs.query.HonorsQuery;
import com.neuedu.funlifebbs.service.HonorsService;
import com.neuedu.funlifebbs.utils.JsonModel;

/**
 * @author weiminqiao
 * @version 创建时间：2019年8月19日 下午8:26:52
 */

@Controller
@RequestMapping("admin/honors")
public class HonorsController {
	@Autowired
	HonorsService honorsService;

	/**
	 * 转发到勋章列表页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "/listUi", method = RequestMethod.GET)
	public String listAll() {
		return "back/honors/list";
	}

	/**
	 * 分页查询并显示在勋章列表页面
	 * 
	 * @param honorsQuery
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	@ResponseBody
	public JsonModel list(HonorsQuery honorsQuery) throws IOException {
		PageInfo<Honors> pageInfo = honorsService.query(honorsQuery);
		JsonModel jsonModel = new JsonModel();
		jsonModel.setMsg("查找成功");
		jsonModel.setSuccess(true);
		jsonModel.setTotal(pageInfo.getTotal());
		jsonModel.setRows(pageInfo.getList());
		return jsonModel;
	}

	/**
	 * 转发到添加勋章页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String addUi() {
		return "back/honors/add";
	}

	/**
	 * * 添加新的勋章,上传勋章图片
	 * 
	 * @param honors
	 * @param request
	 * @param pictureFile
	 * @return
	 */

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public String add(Honors honors, HttpServletRequest request,
			MultipartFile pictureFile) {
		// 使用UUID给图片重命名，并去掉四个“-”
		String name = UUID.randomUUID().toString().replaceAll("-", "");
		// 获取文件的扩展名
		String ext = FilenameUtils.getExtension(pictureFile
				.getOriginalFilename());
		// 设置图片上传路径
		String url = request.getSession().getServletContext()
				.getRealPath("/upload");
		System.out.println(url);
		// 以绝对路径保存重名命后的图片
		try {
			pictureFile.transferTo(new File(url + "/" + name + "." + ext));
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		// 把图片存储路径保存到数据库
		honors.setPicture("upload/" + name + "." + ext);

		if (honors.getId() != 0) {
			honorsService.update(honors);
		} else {
			honorsService.add(honors);
		}
		return "redirect:/admin/honors/listUi";
	}

	/**
	 * 根据勋章ID编辑勋章信息
	 * 
	 * @param model
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
	public String editUi(Model model, @PathVariable int id) {
		Honors honors = honorsService.findById(id);
		model.addAttribute("honors", honors);
		return "back/honors/edit";
	}

	/**
	 * 根据勋章ID删除某个勋章
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
	@ResponseBody
	public JsonModel delete(@PathVariable int id) {
		honorsService.deleteById(id);
		JsonModel jsonModel = new JsonModel();
		jsonModel.setSuccess(true);
		jsonModel.setMsg("删除成功");
		return jsonModel;
	}
}
